<?php

class Listing extends CI_Controller {

    var $_public_view;

    function __construct() {
        parent::__construct();
        $this->load->model('model_listing', 'mlisting');
        $this->load->helper('url', 'site');
        $this->load->helper('form');
        $this->load->library('session');
        $this->load->library('googlemaps');
        $this->config->load('artyproperty');
        $this->_public_view = $this->config->item('public_view');
    }

    function index() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array('daftar' => $this->mlisting->get_listing(),
            'page_title' => 'Properti',
            'page_content' => 'view_listing',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    function detail() {
        $config['center'] = '0.429, 0.1419';
        $config['zoom'] = 'auto';
        $this->googlemaps->initialize($config);
        $marker = array();
        $marker['position'] = '0.429, 0.1419';
        $this->googlemaps->add_marker($marker);

        $data_output = array();
        $str_id = '';
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        if ($this->uri->segment(3) == FALSE) {
            redirect('listing', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }

        $ip = $this->input->ip_address();
        $sql = "SELECT * FROM hit WHERE ip_address='" . $ip
                . "' AND properti_id='" . $str_id . "'";
        $count = $this->db->query($sql);
        if ($count->num_rows() < 1) {
            $sql = "INSERT INTO hit VALUES($str_id,'" . $ip . "')";
            $this->db->query($sql);
        }

        $sql = "SELECT * FROM properti WHERE properti_id=" . $this->uri->segment(3);
        $query = $this->db->query($sql);
        if ($query->num_rows() == 1) {
            $prop = $query->row();
            $data_output['properti']['kode'] = $prop->kode;
            $data_output['properti']['nama'] = $prop->nama_properti;
            $data_output['properti']['tgl_upload'] = date('d-m-Y', strtotime($prop->tgl_upload));
            $data_output['properti']['sewa_beli'] = $prop->sewa_beli;
            $data_output['properti']['tipe'] = $prop->tipe;
            $data_output['properti']['harga'] = $prop->harga;
            $data_output['properti']['jml_kmr_tidur'] = $prop->jml_kmr_tidur;
            $data_output['properti']['jml_kmr_mandi'] = $prop->jml_kmr_mandi;
            $data_output['properti']['jml_kmr_pembantu'] = $prop->jml_kmr_pembantu;
            $data_output['properti']['info_sekolah_rs'] = $prop->info_sekolah_rs;
            $data_output['properti']['info_interior'] = $prop->info_interior;
            $data_output['properti']['luas_tanah'] = $prop->luas_tanah;
            $data_output['properti']['luas_bangunan'] = $prop->luas_bangunan;
            $data_output['properti']['tahun_didirikan'] = $prop->tahun_didirikan;
            $data_output['properti']['sertifikasi'] = $prop->sertifikasi;
            $data_output['properti']['furnitur'] = $prop->furnitur;
            $data_output['properti']['deskripsi'] = $prop->deskripsi;

            $sql = "select * from media where media.properti_id = ?";
            $query = $this->db->query($sql, $str_id);
            $count = 0;
            $data_output['properti']['picture'] = array();
            foreach ($query->result() as $row) {
                $data_output['properti']['picture'][$count] = $row->url;
                $count++;
            }


            $sql = "select * from properti, alamat where properti.properti_id = ? and properti.alamat_id = alamat.alamat_id";
            $query = $this->db->query($sql, $prop->properti_id);
            $alamat_prop = $query->row();
            $data_output['properti']['alamat']['jalan'] = $alamat_prop->jalan;
            $data_output['properti']['alamat']['kota_kab'] = $alamat_prop->kota_kab;
            $data_output['properti']['alamat']['provinsi'] = $alamat_prop->provinsi;
            $data_output['properti']['alamat']['long'] = $alamat_prop->longitude;
            $data_output['properti']['alamat']['lat'] = $alamat_prop->latitude;

            $sql = "select * from properti, fasilitas where properti_id = ? and properti.fasilitas_id = fasilitas.fasilitas_id";
            $query = $this->db->query($sql, $prop->properti_id);
            $fasilitas_prop = $query->row();
            $data_output['properti']['fasilitas']['kolam renang'] = $fasilitas_prop->kolam_renang;
            $data_output['properti']['fasilitas']['gym'] = $fasilitas_prop->gym;
            $data_output['properti']['fasilitas']['sauna'] = $fasilitas_prop->sauna;
            $data_output['properti']['fasilitas']['jogging track'] = $fasilitas_prop->jogging_track;
            $data_output['properti']['fasilitas']['function room'] = $fasilitas_prop->function_room;
            $data_output['properti']['fasilitas']['children playground'] = $fasilitas_prop->children_playground;
            $data_output['properti']['fasilitas']['atm'] = $fasilitas_prop->atm;
            $data_output['properti']['fasilitas']['mini market'] = $fasilitas_prop->mini_market;
            $data_output['properti']['fasilitas']['library'] = $fasilitas_prop->library;
            $data_output['properti']['fasilitas']['tennis court'] = $fasilitas_prop->tennis_court;
            $data_output['properti']['fasilitas']['shopping center'] = $fasilitas_prop->shopping_center;
            $data_output['properti']['fasilitas']['entertainment center'] = $fasilitas_prop->entertainment_center;
        } else {
            echo 'Detail rumah tidak ditemukan...';
            redirect('home', 'refresh');
        }


        $sql = "select * from user, properti where user.user_id = properti.user_id 
                and properti.properti_id = ?";
        $query = $this->db->query($sql, $str_id);
        $agen = $query->row();
        $data_output['agen']['nama_depan'] = $agen->nama_depan;
        $data_output['agen']['nama_belakang'] = $agen->nama_belakang;
        $data_output['agen']['no_hp'] = $agen->no_hp;
        $data_output['agen']['pin_bb'] = $agen->pin_bb;

        $sql = "select * from user, alamat where user.user_id = ? and user.alamat_id = alamat.alamat_id";
        $query = $this->db->query($sql, $agen->user_id);
        $alamat_agen = $query->row();
        $data_output['agen']['alamat']['jalan'] = $alamat_agen->jalan;
        $data_output['agen']['alamat']['kota_kab'] = $alamat_agen->kota_kab;
        $data_output['agen']['alamat']['provinsi'] = $alamat_agen->kota_kab;

        $sql = "select * from media where media.user_id = ?";
        $query = $this->db->query($sql, $agen->user_id);
        $data_output['agen']['picture'] = "";
        if ($query->num_rows() == 1) {
            $pic_agen = $query->row();
            $data_output['agen']['picture'] = $pic_agen->url;
        }

        $sql = "select * from properti, user where user.user_id = properti.user_id";
        $query = $this->db->query($sql);
        $data_output['agen']['jml_listing'] = $query->num_rows();

        $sql = "select * from user, perusahaan where user.perusahaan_id = perusahaan.perusahaan_id 
                and user.user_id = ?";
        $query = $this->db->query($sql, $agen->user_id);
        $perusahaan = $query->row();
        $data_output['perusahaan']['nama'] = $perusahaan->nama_perusahaan;
        $data_output['perusahaan']['no_telp'] = $perusahaan->telp_kantor;
        $data_output['perusahaan']['fax'] = $perusahaan->fax;

        $sql = "select * from perusahaan, alamat where perusahaan.perusahaan_id = ? and perusahaan.alamat_id = alamat.alamat_id";
        $query = $this->db->query($sql, $perusahaan->perusahaan_id);
        $alamat_perusahaan = $query->row();
        $data_output['perusahaan']['alamat']['jalan'] = $alamat_perusahaan->jalan;
        $data_output['perusahaan']['alamat']['kota_kab'] = $alamat_perusahaan->kota_kab;
        $data_output['perusahaan']['alamat']['provinsi'] = $alamat_perusahaan->provinsi;
        $data_output['perusahaan']['alamat']['longitude'] = $alamat_perusahaan->longitude;
        $data_output['perusahaan']['alamat']['latitude'] = $alamat_perusahaan->latitude;

        $sql = "select * from media where media.perusahaan_id = ?";
        $query = $this->db->query($sql, $perusahaan->perusahaan_id);
        $data_output['perusahaan']['picture'] = "";
        if ($query->num_rows() == 1) {
            $pic_perusahaan = $query->row();
            $data_output['perusahaan']['picture'] = $pic_perusahaan->url;
        }

        // if ($query->num_rows > 0) {
        $data = array(
            'output' => $data_output,
            'page_content' => 'detail_listing',
            'page_title' => 'Detail Properti',
            'isLogin' => $islogin,
            'user' => $user,
            'query' => $query,
            'map' => $this->googlemaps->create_map());
        $this->load->view($this->_public_view, $data);
        // } else {
        //     redirect('home', 'location');
        // }
    }

    /**
     * menampilkan halaman untuk cari properti
     */
    function cari() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }
        $this->session->set_userdata('page', 2);
        $data = array(
            'page_content' => 'cari_properti',
            'page_title' => 'cari properti',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    /**
     * menampilkan daftar properti baru
     */
    function baru() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array(
            'page_content' => 'properti_baru',
            'page_title' => 'properti baru',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    /**
     * menampilkan daftar properti premium lainnya
     */
    function premium() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array(
            'page_content' => 'properti_baru',
            'page_title' => 'properti premium lainnya',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

    /**
     * menampilkan daftar properti baru lainnya
     */
    function baru_lainnya() {
        //cek session
        $islogin = $this->session->userdata('isLogin');
        $user = '';
        if ($islogin) {
            $user = $this->session->userdata('username');
        }

        $data = array(
            'page_content' => 'properti_baru',
            'page_title' => 'properti baru lainnya',
            'isLogin' => $islogin,
            'user' => $user);
        $this->load->view($this->_public_view, $data);
    }

}

?>